package com.codi.bus.core.service.model;

import com.codi.base.util.DateUtils;
import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.constant.RuleConstant;
import com.codi.bus.core.dao.LookupDao;
import com.codi.bus.core.domain.Lookup;
import com.codi.bus.core.domain.User;
import com.codi.bus.core.resultModel.AssetsResult;
import com.codi.bus.core.resultModel.BankListResult;
import com.codi.bus.core.resultModel.BaseListResult;
import com.codi.bus.core.service.MessageService;
import com.codi.bus.core.service.TradeService;
import com.codi.bus.core.service.util.HundsunUtils;
import com.codi.bus.core.service.util.ModelPopulateUtils;
import com.codi.bus.exception.BaseException;
import com.codi.fcloud.ufx.constant.HsConst;
import com.codi.trade.dto.HsDividendQryDto;
import com.codi.trade.service.DividendsQueryService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by asus on 2018/4/12.
 */

@Slf4j
public class SyncDividendThread extends Thread{

    private DividendsQueryService dividendsQueryService;

    private MessageService messageService;

    private LookupDao lookupDao;

    private Date beginCreateDate;

    private Date endCreateDate;

    private User user;

    public SyncDividendThread(DividendsQueryService dividendsQueryService, MessageService messageService, LookupDao lookupDao, Date beginCreateDate, Date endCreateDate, User user) {
        this.dividendsQueryService = dividendsQueryService;
        this.messageService = messageService;
        this.lookupDao = lookupDao;
        this.beginCreateDate = beginCreateDate;
        this.endCreateDate = endCreateDate;
        this.user = user;
    }


    @Override
    public void run() {
        log.info("sync dividend run time :{}, user:{}",beginCreateDate,user.getUserId());

        //查询分红记录
        HsDividendQryDto queryDto = HsDividendQryDto.builder()
            .client_id(user.getExternalClientId())
            .qry_beginrownum(1)
            .request_num(GlobalConstant.MAX_REQUEST_NUM)
            .sort_direction(HsConst.SORT_DIRECTION_DESC)
            .confirmdate_begin(DateUtils.formatDate4(beginCreateDate))
            .confirmdate_end(DateUtils.formatDate4(endCreateDate))
            .build();
        Map<String, Object> map = dividendsQueryService.queryDto(queryDto);
        //Map<String, Object> map = dividendsQueryService.query(GlobalConstant.MAX_REQUEST_NUM, 1, 1, "1", "", "", user.getExternalClientId(), "");
        List<Map<String, Object>> list = new ArrayList<>();
        AssetsResult result = new AssetsResult();
        HundsunUtils.ConvertToList(map, list, result);

        if (!result.getSuccess()) {
            throw new BaseException(result.getErrorCode(), result.getErrorMessage());
        }
        BaseListResult baseListResult = new BankListResult();
        List<DividendModel> dividends = ModelPopulateUtils.populateDividend(list, baseListResult);

        //插入消息表
        for (DividendModel dividendModel : dividends){
            try {
                messageService.updateDividendUserMsg(dividendModel,user);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
